package org.example.Server.service.examination.impl;

import org.example.Server.mapper.CommenEnumMapper;
import org.example.Server.mapper.examination.TeachExamDataMapper;
import org.example.Server.model.examination.Exam;
import org.example.Server.model.examination.data.*;
import org.example.Server.model.examination.qs.ExamOption;
import org.example.Server.model.examination.qs.QsExam;
import org.example.Server.model.examination.qs.QsExamDTO;
import org.example.Server.model.examination.qs.QsExamVO;
import org.example.Server.model.commen.QsPoint;
import org.example.Server.service.examination.TeachExamDataService;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TeachExamDataServiceImpl implements TeachExamDataService {
    @Autowired
    TeachExamDataMapper teachExamDataMapper;
    @Autowired
    CommenEnumMapper commenEnumMapper;

    /**
     * 获取试卷概览：总人数、平均分。。。
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getPaperdata(Exam exam) {
//        ResultAPI result = null;
        PaperData paperData = teachExamDataMapper.getPaperdata(exam);
        return ResultAPI.success(paperData);
    }

    /**
     * 获取考试具体数据
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getDivisionData(Exam exam) {
        DivisionRes divisionRes = new DivisionRes();
        List<Double> qsAverList = teachExamDataMapper.getQsAverList(exam);
        List<DivisionData> divisionDatas = teachExamDataMapper.getDivisionData(exam);
        divisionRes.setDivisionDatas(divisionDatas);
        divisionRes.setQsAvers(qsAverList);
        return ResultAPI.success(divisionRes);
    }

    /**
     * 获取知识点词云
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getPointData(Exam exam) {
        List<PointData> pointDatas = teachExamDataMapper.getPointDatas(exam);
        return ResultAPI.success(pointDatas);
    }

    /**
     * 获取优秀良好及格不及格人数比例
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getScoredata(Exam exam) {
        Map<String, Long> stuScoreDatas = teachExamDataMapper.getScoredata(exam);
        List<StuScoreData> res = stuScoreDatas.entrySet().stream()
                .map(entry -> {
                    StuScoreData item = new StuScoreData();
                    item.setName(entry.getKey());
                    item.setValue(entry.getValue());
                    return item;
                }).collect(Collectors.toList());
        return ResultAPI.success(res);
    }

    /**
     * 获取考试试卷题目列表
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getExamQsList(Exam exam) {
        List<QsExam> qsExams = teachExamDataMapper.getExamQsList(exam);
        return ResultAPI.success(qsExams);
    }

    /**
     * 获取单个题的具体内容（具体题型具体分析
     *
     * @param exam
     * @return
     */
    @Override
    public ResultAPI getQs(QsExamDTO exam) {
        String qsType = exam.getQs_type();
        Integer qsId = exam.getQs_id();
        QsExamVO qsExamVO = new QsExamVO();
        // 查具体
        if (qsType.equals("qna")) {
            qsExamVO = teachExamDataMapper.getExamQsQna(exam);
        } else {
            List<ExamOption> examOption = teachExamDataMapper.getExamQsSel(exam);
            if (examOption != null) {
                qsExamVO.setOptions(examOption);
            }
        }
        if (qsExamVO != null) {
            // 查知识点
            QsPoint qsPoint = commenEnumMapper.getPointByQsId(qsId);
            qsExamVO.setQsPoint(qsPoint);
        }
        return ResultAPI.success(qsExamVO);
    }

}
